/**
 * @package com.esun.rabbit2.communicaters.hemai
 * @description 取得合买列表通信类
 * @author liugan
 * @data 2012-03-14 13:58
 * 
 */

package com.esun.rabbit2.communicaters.hemai;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.client.ClientProtocolException;
import org.xml.sax.SAXException;

import android.text.TextUtils;

import com.esun.config.BussinessCode;
import com.esun.config.Key;
import com.esun.model.HeMaiListInfo;
import com.esun.model.UserInfo;
import com.esun.network.ServerCommunicater;

/**
 * @package
 * @description 取得合买列表通信类
 * @author liugan
 * @data 2012-03-14 13:58
 * 
 */
public class HemaiGetListCommunicater extends ServerCommunicater {

	/**
	 * @field serverMsg
	 * @description 服务器返回的消息
	 */
	private String serverMsg = "";
	private ArrayList<HeMaiListInfo> hemaiList = new ArrayList<HeMaiListInfo>();

	/**
	 * @title createParamMap
	 * @description 构造参数的HashMap
	 * @param name
	 *            名
	 * @param value
	 *            值
	 * @return 参数HashMap
	 */
	private HashMap<String, String> createParamMap(String name, String value) {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put(name, value);
		return map;
	}

	/**
	 * @title isGetHemaiListSucceed
	 * @description 获取合买列表
	 * @param mId
	 *            业务代码
	 * @param lotId
	 *            彩种代码
	 * @param playId
	 *            玩法代码
	 * @param expect
	 *            期号
	 * @param sortField
	 *            列表的排序类型
	 * @param sortOrder
	 *            列表的排序方式
	 * @param pageSize
	 *            页面大小
	 * @param pageNo
	 *            页码
	 * @return 是否成功获取列表
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	public boolean isGetHemaiListSucceed(String mId, String lotId, String playId, String expect, String sortField, String sortOrder, String pageSize, String pageNo) throws ClientProtocolException,
			IOException, ParserConfigurationException, SAXException {
		boolean isSucceed = false;
		// url参数
		ArrayList<HashMap<String, String>> paramsList = new ArrayList<HashMap<String, String>>();
		paramsList.add(createParamMap(BussinessCode.PASER_MID, mId));
		paramsList.add(createParamMap(BussinessCode.PASER_CK, UserInfo.getUserInfo().getUserSession()));
		if(!TextUtils.isEmpty(expect)){
			paramsList.add(createParamMap(Key.KEY_EXPECT, expect));
		}
		paramsList.add(createParamMap(Key.KEY_LOTID, lotId));

		if (!TextUtils.isEmpty(playId)) {
			paramsList.add(createParamMap(Key.KEY_PLAYID, playId));
		}
		paramsList.add(createParamMap(Key.KEY_SORTFIELD, sortField));
		paramsList.add(createParamMap(Key.KEY_SORTORDER, sortOrder));
		paramsList.add(createParamMap(Key.KEY_PAGESIZE, pageSize));
		paramsList.add(createParamMap(Key.KEY_PAGENO, pageNo));
		// 写入服务器返回信息
		super.getServerResponse(paramsList, true);
		setServerMessage(super.getServerMsg());
		if (super.getState() == 100) {
			isSucceed = true;
			createHemaiInfoList();
		} else {
			isSucceed = false;
		}
		return isSucceed;
	}

	/**
	 * @title createHemaiInfoList
	 * @description 生成合买记录列表
	 */
	private void createHemaiInfoList() {

		ArrayList<Hashtable<String, String>> originalList = super.getListResult();

		HeMaiListInfo hemaiInfo;
		Hashtable<String, String> tempInfo;
		for (int i = 0; i < originalList.size(); i++) {

			tempInfo = originalList.get(i);
			hemaiInfo = new HeMaiListInfo();
			hemaiInfo.setID(tempInfo.get(Key.KEY_ID));
			hemaiInfo.setAllMoney(tempInfo.get(Key.KEY_ALLMONEY));
			hemaiInfo.setProID(tempInfo.get(Key.KEY_PROID));
			hemaiInfo.setPlayType(tempInfo.get(Key.KEY_PLAYTYPE));
			hemaiInfo.setUserName(tempInfo.get(Key.KEY_USERNAME));
			hemaiInfo.setZhanJi(tempInfo.get(Key.KEY_ZHANJI));
			hemaiInfo.setJinDu(tempInfo.get(Key.KEY_RENQI));
			hemaiInfo.setAllNumber(tempInfo.get(Key.KEY_ANUMBER));
			hemaiInfo.setOneMoney(tempInfo.get(Key.KEY_ONEMONEY));
			hemaiInfo.setBuyNumber(tempInfo.get(Key.KEY_HNUMBER));
			hemaiInfo.setOrderOrNot(tempInfo.get(Key.KEY_SETORDER));
			hemaiInfo.setShowId(tempInfo.get(Key.KEY_SHOW_ID));
			hemaiInfo.setJinzuan(tempInfo.get(Key.KEY_ZHANJI_JINZUAN));
			hemaiInfo.setYinzuan(tempInfo.get(Key.KEY_ZHANJI_YINZUAN));
			hemaiInfo.setJinxing(tempInfo.get(Key.KEY_ZHANJI_JINXING));
			hemaiInfo.setYinxing(tempInfo.get(Key.KEY_ZHANJI_YINXING));
			hemaiInfo.setJinbei(tempInfo.get(Key.KEY_ZHANJI_JINBEI));

			hemaiList.add(hemaiInfo);
		}

	}

	private void setServerMessage(String str) {
		serverMsg = str;
	}

	public String getServerMsg() {
		return serverMsg;
	}

	public ArrayList<HeMaiListInfo> getHeMaiList() {
		return this.hemaiList;
	}
}
